package com.personal_workspace_java.service.impl;

import ch.qos.logback.core.util.StringUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.personal_workspace_java.common.PageResponse;
import com.personal_workspace_java.common.Query;
import com.personal_workspace_java.common.SuperUtil;
import com.personal_workspace_java.exception.CustomException;
import com.personal_workspace_java.mapper.HotPostMapper;
import com.personal_workspace_java.model.dto.HotPostDto;
import com.personal_workspace_java.model.en.HotPost;
import com.personal_workspace_java.model.vo.HotPostVo;
import com.personal_workspace_java.service.HotPostService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author 19324
*/
@Service
public class HotPostServiceImpl  extends ServiceImpl<HotPostMapper, HotPost>
        implements HotPostService {

    @Resource
    private HotPostMapper hotPostMapper;

    //1.新增HotPost
    @Override
    public void create(HotPostDto hotPostDto) {
        //1.获取参数
        Long id= hotPostDto.getId();
        String name= hotPostDto.getName();
        String type= hotPostDto.getType();
        String typeName= hotPostDto.getTypeName();
        String iconUrl= hotPostDto.getIconUrl();
        String hostJson= hotPostDto.getHostJson();
        Integer category= hotPostDto.getCategory();
        BigDecimal updateInterval= hotPostDto.getUpdateInterval();
        Integer sort= hotPostDto.getSort();
        //2.判断是否存在
        QueryWrapper<HotPost> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id" ,id);
        queryWrapper.eq("name" ,name);
        queryWrapper.eq("type" ,type);
        queryWrapper.eq("type_name" ,typeName);
        queryWrapper.eq("icon_url" ,iconUrl);
        queryWrapper.eq("host_json" ,hostJson);
        queryWrapper.eq("category" ,category);
        queryWrapper.eq("updateInterval" ,updateInterval);
        queryWrapper.eq("sort" ,sort);
        HotPost hotPost = hotPostMapper.selectOne(queryWrapper);
        if (hotPost != null) {
            throw new CustomException("数据已存在");
        }
        //3.封装对象
        hotPost = new HotPost();
        hotPost.setId(id);
        hotPost.setName(name);
        hotPost.setType(type);
        hotPost.setTypeName(typeName);
        hotPost.setIconUrl(iconUrl);
        hotPost.setHostJson(hostJson);
        hotPost.setCategory(category);
        hotPost.setUpdateInterval(updateInterval);
        hotPost.setSort(sort);
        hotPostMapper.insert(hotPost);
    }


    //2.查询ByHotPostId
    @Override
    public HotPostVo queryByHotPostId(HotPostDto hotPostDto) {
        //1.获取参数
        Long hotPostId = hotPostDto.getId();
        if(hotPostId == null){
            throw new CustomException("查询参数不能为空");
        }
        //2.查询
        HotPost hotPost = hotPostMapper.selectById(hotPostId);
        if(hotPost == null){
            throw new CustomException("用户不存在");
        }
        //3.封装对象
        return BeanUtil.copyProperties(hotPost, HotPostVo.class);
    }

    //3.查询By其他字段
    @Override
    public HotPostVo queryByOther(HotPostDto hotPostDto) {
        //1.获取参数
        List<Query> queryList = hotPostDto.getQueryList();
            if(queryList == null){
                throw new CustomException("查询参数不能为空");
            }
        //2.查询
        Map<String, Object> queryMap = SuperUtil.listToMap(queryList);
        QueryWrapper<HotPost> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(queryMap);
        HotPost hotPost= hotPostMapper.selectOne(queryWrapper);
        if(hotPost == null){
            throw new CustomException("用户不存在");
        }
        //3.封装对象
        return BeanUtil.copyProperties(hotPost, HotPostVo.class);
    }
    //4.查询所有
    @Override
    public List<HotPostVo> queryAll() {
        //1.查询
        List<HotPost> HotPosts = hotPostMapper.selectList(null);
        // 2. 封装对象
        return HotPosts.stream().map(HotPost -> BeanUtil.copyProperties(HotPost, HotPostVo.class)).collect(Collectors.toList());
    }


    //5.删除ByHotPostId
    @Override
    public void deleteByHotPostId(HotPostDto hotPostDto) {
        //1.获取参数
        Long hotPostId = hotPostDto.getId();
        if(hotPostId == null){
            throw new CustomException("删除参数不能为空");
        }
        //2.删除
        hotPostMapper.deleteById(hotPostId);
        if(hotPostMapper.selectById(hotPostId) != null){
            throw new CustomException("删除失败");
        }
    }


    //6.修改ByHotPostId
    @Override
    public void updateByHotPostId(HotPostDto hotPostDto) {
        //1.获取ID
        Long hotPostId = hotPostDto.getId();
        //2.获取参数
        String name= hotPostDto.getName();
        String type= hotPostDto.getType();
        String typeName= hotPostDto.getTypeName();
        String iconUrl= hotPostDto.getIconUrl();
        Integer category= hotPostDto.getCategory();
        Integer sort= hotPostDto.getSort();
        if(hotPostId == null){
            throw new CustomException("修改参数不能为空");
        }
        //3.修改
        LambdaUpdateWrapper<HotPost> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(HotPost::getId, hotPostId);
        lambdaUpdateWrapper.set(StringUtil.isNullOrEmpty(name),HotPost::getName ,name);
        lambdaUpdateWrapper.set(StringUtil.isNullOrEmpty(type),HotPost::getType ,type);
        lambdaUpdateWrapper.set(StringUtil.isNullOrEmpty(typeName),HotPost::getTypeName ,typeName);
        lambdaUpdateWrapper.set(StringUtil.isNullOrEmpty(iconUrl),HotPost::getIconUrl ,iconUrl);
        lambdaUpdateWrapper.set(category!=null,HotPost::getCategory ,category);
        lambdaUpdateWrapper.set(sort!=null,HotPost::getSort ,sort);
        hotPostMapper.update(null, lambdaUpdateWrapper);
    }

    @Override
    public PageResponse<HotPostVo> queryHotPostByPage(HotPostDto hotPostDto) {
        //1.获取参数
        String sortBy = hotPostDto.getSortBy();
        Boolean isAsc = hotPostDto.getIsAsc();
        Page<HotPost> page = hotPostDto.toPageDefaultSortByUpdateTime();
        if(StrUtil.isNotBlank(sortBy)&& isAsc != null){
            page = hotPostDto.toPage(sortBy, isAsc);
        }else if(StrUtil.isNotBlank(sortBy)){
            page = hotPostDto.toPageWithSortBy(sortBy);
        }else if(isAsc != null){
            page = hotPostDto.toPageWithIsAsc(isAsc);
        }

        List<Query> queryList = hotPostDto.getQueryList();
        List<String> serachList = hotPostDto.getSearchList();
        //2.查询
        QueryWrapper<HotPost> queryWrapper = new QueryWrapper<>();
        if(queryList != null && !queryList.isEmpty()){
            Map<String, Object> queryMap = SuperUtil.listToMap(queryList);
            queryWrapper.allEq(queryMap);
        }

        // 处理搜索字段
        if (serachList != null && !serachList.isEmpty()) {
            String searchName = hotPostDto.getSearchName(); // 假设 hotPostDto 中有 searchValue 字段
            queryWrapper.and(wrapper -> {
                for (String field : serachList) {
                    wrapper.or().like(field, searchName);
                }
            });
        }
        List<HotPost> hotPosts = hotPostMapper.selectList(queryWrapper);
        //3.封装
        List<HotPostVo> hotPostVos = hotPosts.stream().map(hotPost -> BeanUtil.copyProperties(hotPost, HotPostVo.class)).collect(Collectors.toList());
        return PageResponse.of(page,hotPostVos);
    }

}